package com.feng.show.service.impl;

import com.feng.show.comm.ServerResponse;
import com.feng.show.dao.ProductTypeMapper;

import com.feng.show.entity.ProductType;
import com.feng.show.entity.ProductTypeExample;
import com.feng.show.service.IAddProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service("IAddProductTypeService")
public  class AddProductTypeServiceImpl implements IAddProductTypeService {

    @Autowired
    private ProductTypeMapper mapper;


    @Override
    public ServerResponse<String> addProductType(String name, String no, String des) {


        long count=serarchByName(name);
        if(count>0){
            return  ServerResponse.createBySuccessMsg(name+"已存在，不能重复添加");
        }


        ProductType type=new ProductType();
        type.setDes(des);
        type.setName(name);
        type.setNo(no);

        int status=mapper.insert(type);
        if(status!=0){
            return ServerResponse.createBySuccessMsg("增加成功");
        }else{
            return ServerResponse.createBySuccessMsg("添加失败");
        }

    }

    @Override
    public Long serarchByName(String name) {
        ProductTypeExample typeExample=new ProductTypeExample();
        ProductTypeExample.Criteria criteria=typeExample.createCriteria() ;
        criteria.andNameEqualTo(name);

        long count=mapper.countByExample(typeExample);
        return count;

    }

    @Override
    public ServerResponse<List<ProductType>> getProductTypeList(String userId) {


      ProductTypeExample pte=new ProductTypeExample();

      pte.createCriteria().andFlagDelEqualTo("0");


        List<ProductType>  list=mapper.selectByExample(pte);
        return ServerResponse.createBySuccesMsg(list);
    }

    @Override
    public ServerResponse<String> modifyProductType(ProductType type) {


        int statue=mapper.updateByPrimaryKeySelective(type);
        if(statue>0){
           return  ServerResponse.createBySuccessMsg("更新成功");
        }else{
            return ServerResponse.createBySuccessMsg("更新失败");
        }

    }

  @Override
  public ServerResponse<String> delProductType(String productTypeId) {

      if(productTypeId==null){
        return  ServerResponse.createByErrorMsg("参数错误");
      }
    Integer pid = null;
      try{
         pid=Integer.valueOf(productTypeId);
      }catch(Exception e){
          return ServerResponse.createByErrorMsg("参数错误");
    }

      ProductType productType=new ProductType();
      productType.setId(pid);
      productType.setUpdateTime(new Date());
      productType.setFlagDel("2");

    int status=mapper.updateByPrimaryKeySelective(productType);
    System.out.println("--------跟新----status-->"+status);
    if(status>0){
      return ServerResponse.createBySuccessMsg("删除成功");
    }else{
      return ServerResponse.createBySuccessMsg("删除失败");
    }

  }
}
